package com.xydtech.workflow.controller;

import com.xydtech.common.annotation.Log;
import com.xydtech.common.core.controller.BaseController;
import com.xydtech.common.core.domain.AjaxResult;
import com.xydtech.common.core.domain.entity.SysDept;
import com.xydtech.common.core.page.TableDataInfo;
import com.xydtech.common.enums.BusinessType;
import com.xydtech.common.utils.DateUtils;
import com.xydtech.common.utils.ShiroUtils;
import com.xydtech.common.utils.poi.ExcelUtil;
import com.xydtech.system.service.ISysDeptService;
import com.xydtech.workflow.domain.WflFlowdefine;
import com.xydtech.workflow.service.IWflFlowdefineService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 流程定义Controller
 *
 * @author JHON
 * @date 2021-04-11
 */
@Controller
@RequestMapping("/workflow/flowdefine")
public class WflFlowdefineController extends BaseController {
    private String prefix = "workflow/flowdefine";

    @Autowired
    private IWflFlowdefineService wflFlowdefineService;
    @Autowired
    private ISysDeptService sysDeptService;


    @RequiresPermissions("workflow:flowdefine:view")
    @GetMapping()
    public String flowdefine() {
        return prefix + "/flowdefine";
    }

    /**
     * 查询流程定义列表
     */
    @RequiresPermissions("workflow:flowdefine:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WflFlowdefine wflFlowdefine) {
        startPage();
        List<WflFlowdefine> list = wflFlowdefineService.selectWflFlowdefineList(wflFlowdefine);
        return getDataTable(list);
    }

    /**
     * 导出流程定义列表
     */
    @RequiresPermissions("workflow:flowdefine:export")
    @Log(title = "流程定义", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WflFlowdefine wflFlowdefine) {
        List<WflFlowdefine> list = wflFlowdefineService.selectWflFlowdefineList(wflFlowdefine);
        ExcelUtil<WflFlowdefine> util = new ExcelUtil<WflFlowdefine>(WflFlowdefine.class);
        return util.exportExcel(list, "flowdefine");
    }

    /**
     * 新增流程定义
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存流程定义
     */
    @RequiresPermissions("workflow:flowdefine:add")
    @Log(title = "流程定义", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WflFlowdefine wflFlowdefine) {
        wflFlowdefine.setCreateUser(ShiroUtils.getUserId());
        return toAjax(wflFlowdefineService.insertWflFlowdefine(wflFlowdefine));
    }

    /**
     * 修改流程定义
     */
    @GetMapping("/edit/{flowId}")
    public String edit(@PathVariable("flowId") Long flowId, ModelMap mmap) {
        WflFlowdefine wflFlowdefine = wflFlowdefineService.selectWflFlowdefineById(flowId);
        mmap.put("wflFlowdefine", wflFlowdefine);
        return prefix + "/edit";
    }

    /**
     * 修改保存流程定义
     */
    @RequiresPermissions("workflow:flowdefine:edit")
    @Log(title = "流程定义", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WflFlowdefine wflFlowdefine) {
        wflFlowdefine.setUpUser(ShiroUtils.getUserId());
        wflFlowdefine.setUpTime(DateUtils.getNowDate());
        return toAjax(wflFlowdefineService.updateWflFlowdefine(wflFlowdefine));
    }

    /**
     * 删除流程定义
     */
    @RequiresPermissions("workflow:flowdefine:remove")
    @Log(title = "流程定义", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(wflFlowdefineService.deleteWflFlowdefineByIds(ids));
    }

    /**
     * 流程版本管理
     */
    @RequiresPermissions("workflow:flowdefine:list")
    @GetMapping("/flowversion/{flowId}")
    public String flowVersion(@PathVariable("flowId") Long flowId, ModelMap mmap) {
        mmap.put("wflFlowdefine", wflFlowdefineService.selectWflFlowdefineById(flowId));
        return "workflow/flowversion/flowversion";
    }

    /**
     * 根据法人查询流程下拉框列表
     * @Title: flowList
     * @param deptId
     * @return com.xydtech.common.core.domain.AjaxResult
     * @exception
     * @author JHON
     * @Date 2021/12/30 14:50
     **/
    @PostMapping("/flowList")
    @ResponseBody
    public AjaxResult flowList(Long deptId) {
        WflFlowdefine wflFlowdefine = new WflFlowdefine();
        wflFlowdefine.setDeptId(deptId);
        List<WflFlowdefine> list = wflFlowdefineService.selectWflFlowdefineList(wflFlowdefine);
        return AjaxResult.success(list);
    }

    /**
     *根据流程流程类型和法人机构查询流程信息
     * @Title: flowList
     * @param flowType 流程类型授信审批流程-1,放款审批流程-2,贷后检查流程-3,正常还款流程-4,
     *                  提前结清流程-5,提前部分还款流程-6,贷款展期流程-7,贷款核销流程-8
     * @param businessId 业务编号
     * @return com.xydtech.common.core.domain.AjaxResult
     * @exception
     * @author JHON
     * @Date 2023/4/11 9:34
     **/
    @PostMapping("/getFlowInfo")
    @ResponseBody
    public AjaxResult flowList(String flowType, String businessId) {
        SysDept dept = sysDeptService.selectSecondLevelDeptByDeptId(ShiroUtils.getSysUser().getDeptId());
        WflFlowdefine wflFlowdefine = new WflFlowdefine();
        wflFlowdefine.setDeptId(dept.getDeptId());
        wflFlowdefine.setFlowType(flowType);
        List<WflFlowdefine> list = wflFlowdefineService.selectFlowdefineByFlowType(wflFlowdefine);
        if (list.size() > 0) {
            WflFlowdefine flowDefine = list.get(0);
            //打开还款流程类型
            String repayFlowType = "4,5,6";
            //设置流程实例编号,还款流程实例特殊处理
            if (repayFlowType.contains(flowType)) {
                flowDefine.setInstanceId(businessId + "-" + flowType + DateUtils.dateTimeSim());
            } else if ("8".equals(flowType)) {
                //贷款核销流程
                flowDefine.setInstanceId(businessId + "-" + flowType);
            } else {
                flowDefine.setInstanceId(businessId);
            }
            return AjaxResult.success(flowDefine);
        } else {
            return AjaxResult.error("未配置审批流程");
        }
    }

    /**
     * 复制流程
     */
    @RequiresPermissions("workflow:flowdefine:edit")
    @Log(title = "复制流程", businessType = BusinessType.UPDATE)
    @PostMapping("/copy")
    @ResponseBody
    public AjaxResult copy(WflFlowdefine wflFlowdefine) {

        return toAjax(wflFlowdefineService.copyWflFlowdefine(wflFlowdefine));
    }
}
